import 'dart:io';

import 'package:flutter_my_web/pages/music/float_music_widget.dart';
import 'package:flutter_my_web/utils/dis_loading.dart' if (dart.library.js) 'package:flutter_my_web/utils/web_dis_loading.dart' as disloading;
import 'package:flutter_markdown/flutter_markdown.dart' deferred as flutter_markdown;
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_my_web/constants/Global.dart';
import 'package:flutter_my_web/event/change_root_event.dart';
import 'package:flutter_my_web/pages/editarticle/EditarticlePage.dart' deferred as EditarticlePage;
import 'package:flutter_my_web/pages/home/HomePage.dart' deferred as HomePage;
import 'package:flutter_lib/utils/LogUtils.dart';
import 'package:flutter_my_web/utils/lang/translation_service.dart';
import 'package:flutter_my_web/widget/TextWidget.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get/get_navigation/src/root/get_material_app.dart';
import 'package:get/get_navigation/src/routes/transitions_type.dart';
import 'pages/tools/web_plugin_test/web_plugin_test.dart';
import 'utils/Utils.dart';
class GlobalHttpOverrides extends HttpOverrides {
  @override
  HttpClient createHttpClient(SecurityContext? context) {
    return super.createHttpClient(context) ..badCertificateCallback =
        (X509Certificate cert, String host, int port){
      return true;
    };
  }
}
GlobalKey<NavigatorState> navigatorKey = new GlobalKey();
void main() async{
  HttpOverrides.global = GlobalHttpOverrides();
  WidgetsFlutterBinding.ensureInitialized();
  // 滚动性能优化
  // 根据所涉及的频率差异, 启用此标志可以使滚动时的颤动减少多达97%
  GestureBinding.instance.resamplingEnabled = true;

  SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeLeft,
    DeviceOrientation.landscapeRight
  ]);
  await HomePage.loadLibrary();
  runApp(GetMaterialApp(
    // debugShowCheckedModeBanner: false,
    enableLog: true,
    defaultTransition: Transition.fade,
    opaqueRoute: Get.isOpaqueRouteDefault,
    popGesture: Get.isPopGestureEnable,
    locale: TranslationService.locale,
    // locale: Get.deviceLocale,//使用默认的语言环境
    fallbackLocale: TranslationService.fallbackLocale,

    //指定出错时候，默认的语言环境
    translations: TranslationService(),
    home: MyApp(),
  ));
}

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    // SpUtils.sputils_isUseScreenUatils = false;
    Widget app = MaterialApp(
      title: 'FlutterBlog',
      key: navigatorKey,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: WebPluginTestPage(),
    );
    return ScreenUtilInit(
      designSize: const Size(1920, 1024),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (
        BuildContext context, Widget? child) {
        return app;
      },);

    // app=DropDrawBoardPage();
    // return app;

    // WidgetsBinding.instance
    //     .addPostFrameCallback((_) => _insertOverlay(context));


  }
}

void _insertOverlay(BuildContext context) {
  return Overlay.of(context).insert(
    OverlayEntry(builder: (context) {
      final size = MediaQuery.of(context).size;
      return Positioned(bottom: 0, right: 0,left: 0, child: FloatMusicWidget());
    }),
  );
}

class MainPage extends StatefulWidget {

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  @override
  void initState() {
    super.initState();
    disloading.Disloading().disLoading();

    changeRootEventBus.on().listen((event) {
      LogUtils.e("收到信息");
      setState(() {});
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //浮动按钮
      floatingActionButton: FloatingActionButton(
        //浮动按钮图标
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Icon(
              Icons.library_add_outlined,
              color: Colors.white,
            ),
            SimpleText("添加文章",color: Colors.white,fontSize: 8.sp,)
          ],
        ),
        //浮动按钮背景色
        onPressed: () async {
          if (!Global.isRoot){
            showSnackBar("管理员才能添加文章");
            return;
          }
          await flutter_markdown.loadLibrary();
          await EditarticlePage.loadLibrary();
          Get.to(await EditarticlePage.EditarticlePage());
        },
      ),
      //浮动按钮的位置
      floatingActionButtonLocation:
      FloatingActionButtonLocation.endFloat,
      body: SafeArea(child: HomePage.HomePage()),
    );
  }
}

